package com.cryptoneedle.garden.service.garden.doris;


import cn.hutool.v7.core.bean.BeanUtil;
import com.bubbles.engine.data.core.repository.BaseRepository;
import com.bubbles.engine.data.core.service.BaseService;
import com.cryptoneedle.garden.entity.garden.doris.DorisDatabase;
import com.cryptoneedle.garden.key.DatabaseKey;
import com.cryptoneedle.garden.repository.doris.DorisMetadataRepository;
import com.cryptoneedle.garden.repository.garden.doris.DorisDatabaseRepository;
import com.cryptoneedle.garden.service.crud.garden.doris.*;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>description: Doris Database </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@Slf4j
public class DorisDatabaseService extends BaseService<DorisDatabase, DatabaseKey> {

    @Resource
    private DorisDatabaseRepository dorisDatabaseRepository;

    @Override
    public BaseRepository<DorisDatabase, DatabaseKey> getRepository() {
        return this.dorisDatabaseRepository;
    }

    @Autowired
    private AddDorisService add;
    @Autowired
    private DeleteDorisService delete;
    @Autowired
    private FoundDorisService select;
    @Autowired
    private PatchDorisService patch;
    @Autowired
    private SaveDorisService save;

    @Resource
    private DorisMetadataRepository dorisMetadataRepository;

    public List<DorisDatabase> syncDorisDatabase(String catalogName) {
        // 查询所有
        List<DorisDatabase> originList = select.dorisDatabases(catalogName);
        Map<String, DorisDatabase> originMap = Maps.uniqueIndex(originList, DorisDatabase::databaseKeyString);

        // 待同步数据
        List<DorisDatabase> dealList = dorisMetadataRepository.showDatabasesFrom(catalogName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisDatabase.class))
                .toList();
        Map<String, DorisDatabase> dealMap = Maps.uniqueIndex(dealList, DorisDatabase::databaseKeyString);

        // 新增数据
        List<DorisDatabase> addList = dealList.stream().filter(deal -> !originMap.containsKey(deal.databaseKeyString())).toList();

        // 移除数据
        List<DorisDatabase> removeList = originList.stream().filter(item -> !dealMap.containsKey(item.databaseKeyString())).toList();

        // 保存数据
        List<DorisDatabase> saveList = originList
                .stream()
                .filter(origin -> dealMap.containsKey(origin.databaseKeyString()))
                .peek(origin -> {
                    DorisDatabase deal = dealMap.get(origin.databaseKeyString());
                    origin.setCatalogName(deal.getCatalogName())
                            .setDatabaseName(deal.getDatabaseName());
                }).toList();

        add.dorisDatabases(addList);
        save.dorisDatabases(saveList);
        delete.dorisDatabases(removeList);

        return select.dorisDatabases(catalogName);
    }
}